package com.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
	public static Date addHours(Date date, int hour) {
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		ca.add(11, hour);
		return ca.getTime();
	}

	public static Date addMin(Date date, int min) {
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		ca.add(12, min);
		return ca.getTime();
	}

	public static String convertDateString(Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}
	
	public static Date convertStringDate(String date, String format) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return sdf.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public static String getCurrentDateStr() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		return sdf.format(new Date());
	}

	public static Date getDateAfter(Date d, int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(5, now.get(5) + day);
		return now.getTime();
	}

	public static String getDateBefore(Date d, int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(5, now.get(5) - day);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		return sdf.format(now.getTime());
	}

	public static Long millionSeconds() throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		Long millionSeconds = Long.valueOf(sdf.parse(sdf.format(new Date())).getTime());
		return millionSeconds;
	}
	
	/**
	 * 毫秒 转 日期格式的 字符串
	 * @param seconds
	 * @param format
	 * @return
	 */
	public static String millionToDateString(Long seconds,String format) {
		Date date = new Date();
		date.setTime(seconds);
		return convertDateString(date,format);
	}
	
	public static Date millionToDate(Long seconds) {
		Date date = new Date();
		date.setTime(seconds);
		return date;
	}

	public static int dayForWeek(Date date) throws Exception {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int dayForWeek = 0;
		if (c.get(7) == 1) {
			dayForWeek = 7;
		} else {
			dayForWeek = c.get(7) - 1;
		}
		return dayForWeek;
	}

	public static boolean isInDate(Date date, String strDateBegin, String strDateEnd) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String strDate = sdf.format(date);

		int strDateH = Integer.parseInt(strDate.substring(11, 13));
		int strDateM = Integer.parseInt(strDate.substring(14, 16));
		int strDateS = Integer.parseInt(strDate.substring(17, 19));

		int strDateBeginH = Integer.parseInt(strDateBegin.substring(0, 2));
		int strDateBeginM = Integer.parseInt(strDateBegin.substring(3, 5));
		int strDateBeginS = Integer.parseInt(strDateBegin.substring(6, 8));

		int strDateEndH = Integer.parseInt(strDateEnd.substring(0, 2));
		int strDateEndM = Integer.parseInt(strDateEnd.substring(3, 5));
		int strDateEndS = Integer.parseInt(strDateEnd.substring(6, 8));
		if ((strDateH >= strDateBeginH) && (strDateH <= strDateEndH)) {
			if ((strDateH > strDateBeginH) && (strDateH < strDateEndH)) {
				return true;
			}
			if ((strDateH == strDateBeginH) && (strDateM >= strDateBeginM) && (strDateM <= strDateEndM)) {
				return true;
			}
			if ((strDateH == strDateBeginH) && (strDateM == strDateBeginM) && (strDateS >= strDateBeginS)
					&& (strDateS <= strDateEndS)) {
				return true;
			}
			if ((strDateH >= strDateBeginH) && (strDateH == strDateEndH) && (strDateM <= strDateEndM)) {
				return true;
			}
			if ((strDateH >= strDateBeginH) && (strDateH == strDateEndH) && (strDateM == strDateEndM)
					&& (strDateS <= strDateEndS)) {
				return true;
			}
			return false;
		}
		return false;
	}

	public static void main(String[] args) throws Exception {
		System.out.println(dayForWeek(new Date()));
	}

	public static void month() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		c.add(2, 0);
		c.set(5, 1);
		String first = format.format(c.getTime());
		System.out.println("===============本月first day:" + first);

		Calendar ca = Calendar.getInstance();
		ca.set(5, ca.getActualMaximum(5));
		String last = format.format(ca.getTime());
		System.out.println("===============本月last day:" + last);
	}
}
